package com.wgchao.identityrightsmanagement.domain.permission.model;

import com.wgchao.identityrightsmanagement.domain.organization.model.Department;
import com.wgchao.identityrightsmanagement.domain.organization.model.DepartmentOffice;
import com.wgchao.identityrightsmanagement.domain.organization.model.Office;
import com.wgchao.identityrightsmanagement.domain.resource.model.FunctionResources;
import com.wgchao.identityrightsmanagement.domain.resource.model.MenuResources;
import com.wgchao.identityrightsmanagement.domain.resource.model.ProjectResources;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectNotMatchException;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * @ClassName: AssignPermissionsToOfficeService
 * @author: wgchao
 * @createTime: 2020/6/28 4:29 PM
 * @Description: 为职位分配权限
 */
@Component
public class AssignPermissionsToOfficeService {


    public OfficePermissionInformation assign(Office office, Department department, DepartmentOffice departmentOffice, ProjectResources projectResources,
                                              MenuResources menuResources, List<FunctionResources> functionResourcess){
        department.normalException();
        office.normalException();
        projectResources.normalException();
        OfficePermissionInformation officePermissionInformation = new OfficePermissionInformation(departmentOffice.getDepartmentCode(),
                departmentOffice.getOfficeCode(), projectResources.getProMark(), menuResources.getMenuMark());
        if(functionResourcess!=null && !functionResourcess.isEmpty()){
            for(FunctionResources functionResources : functionResourcess){
                if(!functionResources.belongToMenu(menuResources.getMenuMark())){
                    throw new ObjectNotMatchException(functionResources.getFuncName()+"功能不属于菜单"+menuResources.getMenuName()+"的功能");
                }
                officePermissionInformation.addFunction(menuResources.getMenuMark(), functionResources.getIdentify().getFuncMark());
            }
        }

        return officePermissionInformation;
    }
}
